import {Injectable} from '@angular/core';
import {HttpService} from "@core/http/http.service";
import * as API from "@api/index";

import {Observable} from "rxjs/Observable";


import {ToolsService} from "@shared/service/tools/tools.service";
import {
  CommonPageVo, SaleRankVO, SNSResult, TrainCourseAttrRespVO, TrainFollowRecord, TrainSaleSignFormRespVo,
  TrainSaleStudentRespVO,
  TrainSchoolSimpleVO,
  TrainTrialRecordRespVO
} from "@interface/index";


//
// import {
//   TrainCourseAttrRespVO,
//   TrainFollowRecord, TrainSaleSignFormRespVo, TrainSaleStudentRespVO, TrainSchoolSimpleVO,
//   TrainTrialRecordRespVO
// } from "@interface/sale/sale.model";
// import {SaleRankVO} from "@interface/train/train.model";


@Injectable()
export class SaleService {

  constructor(
    private httpService: HttpService,
    private toolsService: ToolsService
  ) {
  }

  /**
   * @description 查询销售管理 客户资料
   * @author ouxh-862
   * @param  orgUserId
   * @param  studentId
   * @date 2017-12-27
   */
  getSalesCustomInfo(orgUserId: string, studentId: string): Observable<SNSResult<TrainSaleStudentRespVO>> {
    const url: string = API.GET_SALE_STUDENT + `?orgUserId=${orgUserId}&studentId=${studentId}`;
    // const url = '' ;
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 查询客户跟进记录
   * @author ouxh-862
   * @param  orgUserId ,
   * @param  studentId ,
   * @param  page ,
   * @date 2017-12-27
   */
  getSalesFollowInfo(orgUserId: string, studentId: string, page: number): Observable<SNSResult<CommonPageVo<TrainFollowRecord>>> {
    const url = API.GET_FOLLOW_RECORD_LIST + `?orgUserId=${orgUserId}&studentId=${studentId}&page=${page}&rows=10`;
    // const url = '' ;
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 查询客户试课记录
   * @author ouxh-862
   * @param  orgUserId ,
   * @param  studentId ,
   * @param  page ,
   * @date 2017-12-27
   */
  getSalesTestList(orgUserId: string, studentId: string, page: number): Observable<SNSResult<CommonPageVo<TrainTrialRecordRespVO>>> {
    const url = API.GET_TEST_CLASS_LIST + `?orgUserId=${orgUserId}&studentId=${studentId}&page=${page}&rows=10`;
    // const url = '' ;
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 培训报名
   * @author ouxh-862
   * @date 2017-12-30
   * @param courseId 课程id
   */
  addCustomer(data): Observable<SNSResult<any>> {
    const url = API.ADD_CUSTOMER;
    return this.httpService.postUrlencode(url, data).map((res) => res.json());
  }

  /**
   * @description 培训报名
   * @author ouxh-862
   * @date 2017-12-30
   * @param courseId 课程id
   */
  addFollowRecord(data): Observable<SNSResult<string>> {
    const url = API.ADD_FOLLOW_RECORD;
    return this.httpService.postUrlencode(url, data).map((res) => res.json());
  }

  /**
   * @description 获取销售信息
   * @author lih1157
   * @date 2017-12-27
   */
  getSaleDataStatistics(orgUserId): Observable<SNSResult<SaleRankVO>> {
    const url: string = API.GET_SALE_DATA_STATISTICS + `?orgUserId=${orgUserId}`;
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 获取组织下的课程列表
   * @author baoww765
   * @date 2017-12-30
   */
  getCourseList(orgUserId, properties): Observable<SNSResult<Array<TrainSaleSignFormRespVo>>> {
    const url: string = API.SALES_COURSE_LIST + `?orgUserId=${orgUserId}` +
      `&properties=${properties}`;
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 根据课程ID获取校区列表和班级列表
   * @author baoww765
   * @date 2017-12-30
   */
  getCourseAttr(courseId): Observable<SNSResult<TrainCourseAttrRespVO>> {
    const url: string = API.SALES_CAMPUS_ATTR + `?courseId=${courseId}`;
    return this.httpService.get(url).map(res => res.json());
  };

  /**
   * @description 获取客户列表
   * @author lih1157
   * @date 2017-12-27
   * @param params
   */
  getSalespersonStudentList(params): Observable<SNSResult<CommonPageVo<TrainSaleStudentRespVO>>> {
    const url: string = API.GET_SALESPERSON_STUDENT_LIST + '?' + this.toolsService.param(params);
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 获取机构校区信息
   * @author ouxh-862
   * @date 2017-12-30
   */
  getOrgCampus(orgUserId: string): Observable<SNSResult<Array<TrainSchoolSimpleVO>>> {
    const url: string = API.GET_CAMPUS_LIST + `?orgUserId=${orgUserId}`;
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 办理报名
   * @author lih-1157
   * @date 2017-12-31
   * @param params
   */
  applyRegistration(params): Observable<SNSResult<any>> {
    const url: string = API.POST_APPLYREGISTRATION;
    return this.httpService.postUrlencode(url, params).map((res) => res.json());
  };

  /**
   * @description 获取课程动态表单
   * @author lih-1157
   * @date 2018-1-2
   */
  getCourseFormList(orgUserId, properties): Observable<SNSResult<Array<TrainSaleSignFormRespVo>>> {
    const url: string = API.GET_COURSEFORMLIST + `?orgUserId=${orgUserId}` + `&properties=${properties}`;
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 更改学员状态
   * @author ouxh-862
   * @date 2017-12-30
   * @param data = {
     *  id 记录ID
     *  orgUserId 销售人员所属机构ID
     *  studentId 学员ID
     * status 状态(3:跟进中4:流失)
   * }
   */
  updateStudentStatus(data): Observable<SNSResult<any>> {
    const url: string = API.UPDATE_SALE_SALE_STUDENT_STATUS;
    return this.httpService.putUrlencode(url, data).map((res) => res.json());
  };

  /**
   * @description 添加试课
   * @author baoww765
   * @date 2018-01-02
   * @param courseId 课程id
   */
  addStudentToTestClass(data): Observable<SNSResult<any>> {
    const url = API.SALES_ADD_STUDENT_TO_TESTCLASS;
    return this.httpService.postUrlencode(url, data).map((res) => res.json());
  }
}
